/*************************************************************************
	> File Name: P1104.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-01-08 11:10:55
 ************************************************************************/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

struct Stu {
    string name;
    int y, m, d, id;
} arr[105];

bool cmp(Stu &a, Stu &b) {
    if (a.y != b.y) return a.y < b.y;
    if (a.m != b.m) return a.m < b.m;
    if (a.d != b.d) return a.d < b.d;
    return a.id > b.id;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i].name >> arr[i].y >> arr[i].m >> arr[i].d;
        arr[i].id = i + 1;
    }
    sort(arr, arr + n, cmp);
    for (int i = 0; i < n; i++) {
        cout << arr[i].name << endl;
    }
    return 0;
}
